package com.zmn.plat.admin.controller.online.service.group;

import java.util.*;
import java.util.stream.Collectors;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.common.dictionary.OnlineServiceGroupSysEnum;
import com.zmn.plat.model.entity.company.BaseCompany;
import com.zmn.plat.model.entity.company.BaseCompanyQuery;
import com.zmn.plat.model.vo.online.service.group.OnlineServiceGroupVO;
import com.zmn.plat.utils.bean.BeanCopyUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.stereotype.Controller;
import com.zmn.plat.model.entity.online.service.group.OnlineServiceGroup;
import com.zmn.plat.model.entity.online.service.group.OnlineServiceGroupQuery;

/**
 * 在线客服分组基本信息表
 *
 * @author linfeng
 *
 * @since 2021-03-26 17:21:53
 */
@Controller
@RequestMapping("/online/service/group/")
public class OnlineServiceGroupListController extends OnlineServiceGroupBaseController {

    /**
     * 列表
     * @author linfeng
     * @param query
     * @since 2021-03-26 17:21:53
     */
    @RequestMapping("list")
    @RequiresPermissions("/online/service/group/list.action")
    public ModelAndView list(@ModelAttribute OnlineServiceGroupQuery query){

        List<OnlineServiceGroup> items = onlineServiceGroupService.listPageByQuery(query);

        List<String> subCompanyIdList = items.stream().map(e -> e.getSubCompanyId()).collect(Collectors.toList());
        List<Integer> subCompanyIdData = new ArrayList<>();
        for (String e : subCompanyIdList) {
            List<Integer> innerOrgThrId = StringUtil.convertStrsToList(e);
            subCompanyIdData.addAll(innerOrgThrId);
        }
        subCompanyIdData = subCompanyIdData.stream().distinct().collect(Collectors.toList());
        BaseCompanyQuery baseCompanyQuery = new BaseCompanyQuery();
        baseCompanyQuery.setCompanyIds(subCompanyIdData);
        List<BaseCompany> subCompanyList = new ArrayList<>();
        if (CollectionUtil.isNotNullOrEmpty(subCompanyIdData)) {
            subCompanyList = baseCompanyService.listByQuery(baseCompanyQuery);
        }
        Map<Integer, String> subCompanyMap = new HashMap<>();
        subCompanyMap = subCompanyList.stream().collect(Collectors.toMap(o -> o.getCompanyId(), o -> o.getName()));

        Map<Integer, String> finalSubCompanyMap = subCompanyMap;
        List<OnlineServiceGroupVO> displayData = new ArrayList<>();
        items.forEach(e -> {
            OnlineServiceGroupVO data = new OnlineServiceGroupVO();
            BeanCopyUtils.copyProperties(e, data);
            List<Integer> innerSubCompanyId = StringUtil.convertStrsToList(e.getSubCompanyId());
            StringBuffer str = new StringBuffer();
            for (Integer id : innerSubCompanyId) {
                String name = Objects.isNull(finalSubCompanyMap.get(id)) ? "" + id : finalSubCompanyMap.get(id);
                str.append(name).append("、");
            }

            if (str.length() > 0) {
                data.setSubCompanyIdDisplayName(str.deleteCharAt(str.length() - 1).toString());
            }

            displayData.add(data);
        });


        ModelAndView mav = new ModelAndView("online/service/group/list");
        mav.addObject("items", displayData);
        mav.addObject("vo", query);
        return mav;
    }


    /**
     * 详情
     * @author linfeng
     * @param groupId
     * @since 2021-03-26 17:21:53
     */
    @RequestMapping("detail")
    @RequiresPermissions("/online/service/group/detail.action")
    public ModelAndView detail(@RequestParam(value = "groupId", defaultValue = "0") Integer groupId){
        OnlineServiceGroup onlineServiceGroup;
        if (NumberUtil.isNullOrZero(groupId)) {
            onlineServiceGroup = new OnlineServiceGroup();
        } else {
            onlineServiceGroup = onlineServiceGroupService.findByKey(groupId);
        }

        ModelAndView mav = new ModelAndView("online/service/group/detail");
        mav.addObject("obj", onlineServiceGroup);
        mav.addObject("groupId", groupId);
        mav.addObject("sysList", OnlineServiceGroupSysEnum.list());
        mav.addObject("flag", 1);
        return mav;
    }

    @RequestMapping("judgeHasDefaultServiceGroup")
    @ResponseBody
    @SuppressWarnings("Duplicates")
    public ResponseDTO judgeHasDefaultServiceGroup(@RequestBody OnlineServiceGroupQuery onlineServiceGroupQuery) {
        try {
            Boolean flag = onlineServiceGroupBService.judgeHasDefaultServiceGroup(onlineServiceGroupQuery);
            return ResponseDTO.success(flag, "操作成功！");
        } catch (Exception e) {
            logger.error("judgeHasDefaultServiceGroup error...");
            return ResponseDTO.fail("系统异常");
        }
    }
}
